package controller.link;

import controller.link.game.Died;
import view.client.Console;
import model.Hearer;
import view.client.PanelGame;
import java.io.Serializable;
import java.util.ArrayList;
import model.game.Item;
import controller.Link;
import model.game.Player;
import model.game.Skill;
import model.User;
import model.Attendant;
import model.Server;

public class Ask4Yourself implements Link, Serializable {

    private Player player;

    public Ask4Yourself() {
    }

    public Ask4Yourself(Server server, Attendant at) {
        User user = at.getUser();
        player = user.getPlayer();
        at.send((Link) this);
    }

    public Ask4Yourself(Hearer h) {
        h.send((Link) this);
    }

    @Override
    public void serverUse(Server server, Attendant at) {
        User user = at.getUser();
        player = user.getPlayer();
        if (player.getLife()==0){
            Died died = new Died(server, at);
            died.serverUse(server, at);
        }
        at.send((Link) this);
    }

    @Override
    public void clientUse(Console console) {
        PanelGame pg = console.getPg();
        ArrayList<Skill> skills = console.getSkills();
        ArrayList<Item> items = console.getItems();
        pg.getLblAtributo().setText(player.getFreshAtributes() + "/" + player.getTotalAtributes());
        pg.getLblName().setText(player.getName());
        skills = player.getSkills();
        items = player.getItems();
        pg.getLblVida().setText("" + player.getTotalLife());
        pg.getLblEnergia().setText("" + player.getTotalEnergy());
        pg.getLblResistencia().setText("" + player.getResistence());
        pg.getLblArmadura().setText("" + player.getArmor());
        pg.getLblAgilidade().setText("" + player.getAgility());
        pg.getLblPoderDeFogo().setText("" + player.getFirePower());
        pg.getLblForca().setText("" + player.getStrength());
        pg.getLblAptitude().setText("" + player.getAptitude());
        pg.getLblInteligencia().setText("" + player.getIntelligence());
        pg.getLblVigor().setText("" + player.getVigor());
        pg.getLblLevel().setText("Level " + player.getLevel());
        pg.getLblCoin().setText(player.getMoney()+" moedas");
        pg.getPbVida().setStringPainted(true);
        pg.getPbVida().setMaximum(player.getTotalLife());
        pg.getPbVida().setStringPainted(true);
        pg.getPbVida().setValue(player.getLife());
        pg.getPbVida().setString(Integer.toString(player.getLife()) + "/" + Integer.toString(player.getTotalLife()));
        pg.getPbVida().repaint();
        pg.getPbEnergia().setStringPainted(true);
        pg.getPbEnergia().setMaximum(player.getTotalEnergy());
        pg.getPbEnergia().setStringPainted(true);
        pg.getPbEnergia().setValue(player.getEnergy());
        pg.getPbEnergia().setString(Integer.toString(player.getEnergy()) + "/" + Integer.toString(player.getTotalEnergy()));
        pg.getPbEnergia().repaint();
        pg.getPbXp().setStringPainted(true);
        pg.getPbXp().setMaximum(player.xpToLvl());
        pg.getPbXp().setStringPainted(true);
        pg.getPbXp().setValue(player.getXp());
        pg.getPbXp().setString(Integer.toString(player.getXp()) + "/" + Integer.toString(player.xpToLvl()));
        pg.getPbXp().repaint();
        if ((player.getFreshAtributes() > 0) && (player.getFreshAtributes() < player.getTotalAtributes())) {
            pg.enableAtrButtonsAdd();
        } else if (player.getFreshAtributes() == 0) {
            pg.disableAtrButtonsAdd();
        } else if (player.getFreshAtributes() == player.getTotalAtributes()) {
            pg.enableAtrButtonsAdd();
        }
        if (!pg.getModelItem().isEmpty()) {
            pg.getModelItem().clear();
        }
        for (int i = 0; i < items.size(); i++) {
            pg.getModelItem().add(i, items.get(i).getName());
        }
        pg.getListAction().revalidate();
        pg.getListAction().repaint();
        pg.getListPlace().revalidate();
        pg.getListPlace().repaint();
        pg.getListAction().revalidate();
        pg.getListAction().repaint();
        
        pg.revalidate();
        console.getFrame().repaint();
    }
}
